/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.DataSet;

import com.nc.kyiv.yellow.sociallibrary.DataSetDAO.LocalLibDataSet;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookAuthor;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookRating;
import com.nc.kyiv.yellow.sociallibrary.POJO.LocalLibBook;
import com.nc.kyiv.yellow.sociallibrary.helper.EscapeCharacters;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.AbstractDataSet;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Alena
 */
public class GetLocalLib implements  CommonClassesInterface{
    
    private String userId;
    
    public GetLocalLib(String userID){
        this.userId = userID;
    }

    private String [] mapKeys = {"pageNumber","booksNumber","filterOneValue","filterTwoValue"};
    private MapBuilder helper = new MapBuilder();
    private String resultString = null;
    
    @Override
    public String execute(String parameters) throws Exception {
        
        Map postParameters= helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());
        postParameters.put("userId", userId);
        
        System.out.println(postParameters.toString());
        
        if (postParameters!=null){
            
            AbstractDataSet localLibDS = new LocalLibDataSet();
            List<LocalLibBook> localLib = (List<LocalLibBook>) localLibDS.getDataSet(postParameters);        
            
            System.out.println(localLib);
            
            EscapeCharacters codeDescription = new EscapeCharacters();
            StringBuilder libStr = new StringBuilder();
             
            for (LocalLibBook book : localLib) {
                libStr.append("{");
                libStr.append("\"image\":\"");
                libStr.append(book.getImageURL());
                libStr.append("\",\"avgRating\":\"");
                libStr.append(book.getAvgRating());
                libStr.append("\",\"sumRating\":\"");
                libStr.append(book.getSumRating());
                libStr.append("\",\"currentUserScore\":\"");
                libStr.append(book.getCurrentUserScore());
                libStr.append("\",\"title\":\"");
                libStr.append(book.getTitle());
                libStr.append("\",\"description\":\"");
                libStr.append(codeDescription.EscapeCharactersForJSON(book.getDescription()));
                libStr.append("\",");
                List<BookRating> rating = book.getRating();
                Map ratingMarks = new HashMap<Integer, Integer>();
                for (int i = 1; i < 6; i++) {
                    ratingMarks.put(i, 0);
                }
                for (BookRating br : rating) {
                    ratingMarks.put(br.getRatingScore(), br.getSumVotes());
                }
                for (int i = 1; i < 6; i++) {
                    libStr.append("\"voteFor");
                    libStr.append(i);
                    libStr.append("\":\"");
                    libStr.append(ratingMarks.get(i));
                    libStr.append("\",");
                }
                
                List<BookAuthor> authors = book.getAuthors();
                libStr.append("\"authors\":\"");
                for (BookAuthor bk : authors) {
                    if (bk.getFirstName() != null) {
                        libStr.append(" ");
                        libStr.append(bk.getFirstName());
                    }
                    if (bk.getMiddleName() != null) {
                        libStr.append(" ");
                        libStr.append(bk.getMiddleName());
                    }
                    if (bk.getLastName() != null) {
                        libStr.append(" ");
                        libStr.append(bk.getLastName());
                    }
                    libStr.append(",");
                }
                libStr.append("\",");

                libStr.append("\"ISBN\":\"");
                libStr.append(book.getISBN());
                libStr.append("\",");
                
                libStr.append("\"readStatus\":\"");
                libStr.append(book.getReadStatus());
                libStr.append("\"}");                
              
                
                libStr.append(helper.getResponceSplitter()); 
                
              }   
            resultString=libStr.toString();
            if (resultString.length() > 5) {
                resultString = resultString.substring(0, resultString.length() - 5);
            } else {
                return "failed";
            }
            return resultString;
        } else {
            return "failed";
        }
    }
    
}
